from vec import *
from random import randint 
WIDTH = 600
HEIGHT = 600

BACK_COLOR = (0, 0, 20)
PEN_COLOR = (0, 200, 0)
mtList=[Vec2(100,100),Vec2(200,200),Vec2(300,300),Vec2(400,400)]
ptList=[]
dirList=[Vec2(5,3),Vec2(4,2),Vec2(3,1),Vec2(2,1)]
spd=2
c=0
host=3
gone=0
ready_pt=0

def set_dir():
    global dirList,spd
    for i in range(len(dirList)):
        dirList[i].normalize()
        dirList[i]*=spd
set_dir()

def set_line(n):
    global ptList,mtList
    ptList=[]
    for i in range(-n,n+1):
        a=[]
        for j in range(len(mtList)):
            a.append(mtList[j]+Vec2(i*3,i*3))
        ptList.append(a)

set_line(host)

def turn_clr():
    global PEN_COLOR,BACK_COLOR
    r=randint(100,150)
    g=randint(150,200)
    b=randint(200,250)
    PEN_COLOR=(r,g,b)

def on_key_down(key):
    if key == keys.ESCAPE:
        quit() 
        
def on_mouse_down(pos):
    print("mouse_down", pos)
    
def on_mouse_up(pos):
    print("mouse_up", pos)
 
def update():
    global ptList,mtList,host,gone,ready_pt
    global dirList,spd
    mtList=ptList[host+1][:]
    for i in range(len(ptList)):
        for j in range(len(ptList[i])):
            ptList[i][j]+=dirList[j]
            x=0
            y=0
            if ptList[i][j].x > WIDTH or ptList[i][j].x < 0:
                if ptList[i][j].x > WIDTH:
                    ptList[i][j].x=WIDTH
                else:
                    ptList[i][j].x=0
                x=1
                dirList[j].x *= -1
                turn_clr()
                gone+=1
                if gone==host and ready_pt<=0:
                    set_line(host)
                    gone=0
                    ready_pt=host*2+2
                
            if ptList[i][j].y > HEIGHT or ptList[i][j].y < 0:
                if ptList[i][j].y > HEIGHT:
                    ptList[i][j].y=HEIGHT
                else:
                    ptList[i][j].y=0
                y=1
                dirList[j].y *= -1
                turn_clr()
                gone+=1
                if gone==host and ready_pt<=0:
                    set_line(host)
                    gone=0
                    ready_pt=host*2+2
            if x==0 and y==0:
                ready_pt-=1
            
def draw():  
    screen.fill(BACK_COLOR)
    for i in range(len(ptList)):
        for j in range(len(ptList[i])):
            if j == len(ptList[i]) - 1:
                screen.draw.line(ptList[i][j].raw_get(), ptList[i][0].raw_get(), PEN_COLOR)            
            else:
                screen.draw.line(ptList[i][j].raw_get(), ptList[i][j+1].raw_get(), PEN_COLOR)

